// pages/route/daily/timetable/timetable.js
const app = getApp();
const service = require('../../../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 路线id
    lineId: null,
    // 选择的时间
    date: null,
    // 星期几
    weekday: null,
    // 时间选择器开始时间
    startDate: null,
    // 时间选择器结束时间
    endDate: null,
    // 路线列表
    routeList: [],
    // 地点列表
    startSpotList: [],
    startSpotListPreview: [],
    endSpotList: [],
    endSpotListPreview: [],
    // 选择的出发点
    startSpot: [],
    startSpotPreview: [],
    // 选择的目的地
    endSpot: [],
    endSpotPreview: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let date = new Date(options.chooseTime)
    this.setData({
      lineId: options.lineId,
      chooseTime: options.chooseTime,
      weekday: '星期' + app.utils.parseTime(date, '{a}'),
      date: app.utils.parseTime(date, '{m}-{d}'),
      startDate: app.utils.parseTime(new Date(), '{y}-{m}-{d}'),
      endDate: app.utils.parseTime(date.getTime() + 2592000000, '{m}-{d}')
    });
    this.loadData();
  },

  // 加载数据
  loadData() {
    let that = this;
    let params = {
      lineId: this.data['lineId'],
      chooseTime: this.data['chooseTime']
    }
    service.getConnect(service.api.getFixedRouteSearchResult, params).then(function(res) {
      wx.hideLoading();
      that.setData({
        routeList: res.data
      })
    });
  },

  // 选择时间
  bindDateChange(e) {
    console.log(e.detail.value)
    const date = new Date(e.detail.value);
    this.setData({
      date: app.utils.parseTime(date, '{m}-{d}'),
      chooseTime: app.utils.parseTime(date, '{y}-{m}-{d}'),
      weekday: '星期' + app.utils.parseTime(date, '{a}'),
    })
    this.loadData();
  },

  // 前一天
  bindReduceDay() {
    let date = new Date(this.data.chooseTime).getTime() - 86400000;
    this.setData({
      date: app.utils.parseTime(date, '{m}-{d}'),
      chooseTime: app.utils.parseTime(date, '{y}-{m}-{d}'),
      weekday: '星期' + app.utils.parseTime(date, '{a}'),
    })
    this.loadData();
  },

  // 后一天
  bindPlusDay() {
    let date = new Date(this.data.chooseTime).getTime() + 86400000;
    this.setData({
      date: app.utils.parseTime(date, '{m}-{d}'),
      chooseTime: app.utils.parseTime(date, '{y}-{m}-{d}'),
      weekday: '星期' + app.utils.parseTime(date, '{a}'),
    })
    this.loadData();
  },

  // 选择地点
  bindSpotPickerChange(e) {
    const type = e.currentTarget.dataset.type;
    const addressList = e.currentTarget.dataset.type + 'List'
    this.setData({
      [type]: e.detail.value,
      [addressList]: this.data[addressList + 'Preview']
    })
  },

  // 列变化时
  bindSpotPickerColumnChange(e) {
    const type = e.currentTarget.dataset.type + 'Preview';
    const addressList = e.currentTarget.dataset.type + 'ListPreview'
    const data = {
      [addressList]: this.data[addressList],
      [type]: this.data.startSpot.length !== 0 ? this.data.startSpot : [0, 0]
    }
    data[type][e.detail.column] = e.detail.value;
    if (e.detail.column === 0) {
      switch (data[type][0]) {
        case 0:
          data[addressList][1] = [{
            id: 0,
            name: '罗湖区'
          }, {
            id: 1,
            name: '龙岗区'
          }, {
            id: 2,
            name: '龙华区'
          }, {
            id: 3,
            name: '福田区'
          }, {
            id: 4,
            name: '南山区'
          }]
          break
        case 1:
          data[addressList][1] = [{
            id: 0,
            name: '湾仔'
          }, {
            id: 1,
            name: '新界'
          }, {
            id: 2,
            name: '九龙'
          }]
          break
      }
      data[type][1] = 0
    }
    this.setData(data)
  },

  // 买票
  bindToDetails(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/route/fixed/details/details?lineId=' + item.id +
        '&price=' + item.price +
        '&chooseTime=' + this.data['chooseTime']
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})